package cn.boodqian.airreport;

public class AQI {
	public static final int OUTRANGE = 0xff;
	/* cc: concentration */
	static int Linear(double AQIhigh, double AQIlow, double cchigh, double cclow, double cc) {
		double AQI = (cc-cclow)/(cchigh-cclow)*(AQIhigh-AQIlow)+AQIlow;
		return (int)Math.round(AQI);
	}
	
	public static int PM25(float cc) { /* unit: mg/m^3 */
		int AQI;
		cc *= 1000; /* unit -> ug/m^3 */
		if (cc>=0 && cc<15.5)
		{
			AQI=Linear(50,0,15.4,0,cc);
		}
		else if (cc>=15.5 && cc<35.5)
		{
			AQI=Linear(100,51,35.4,15.5,cc);
		}
		else if (cc>=35.5 && cc<65.5)
		{
			AQI=Linear(150,101,65.4,35.5,cc);
		}
		else if (cc>=65.5 && cc<150.5)
		{
			AQI=Linear(200,151,150.4,65.5,cc);
		}
		else if (cc>=150.5 && cc<250.5)
		{
			AQI=Linear(300,201,250.4,150.5,cc);
		}
		else if (cc>=250.5 && cc<350.5)
		{
			AQI=Linear(400,301,350.4,250.5,cc);
		}
		else if (cc>=350.5 && cc<500.5)
		{
			AQI=Linear(500,401,500.4,350.5,cc);
		}
		else
		{
			AQI=OUTRANGE;
		}
		return AQI;
	}
	
	public static int PM10(float cc) { /* unit: mg/m^3 */
		int AQI;
		cc *= 1000; /* unit -> ug/m^3 */
		if (cc>=0 && cc<55)
		{
			AQI=Linear(50,0,54,0,cc);
		}
		else if (cc>=55 && cc<155)
		{
			AQI=Linear(100,51,154,55,cc);
		}
		else if (cc>=155 && cc<255)
		{
			AQI=Linear(150,101,254,155,cc);
		}
		else if (cc>=255 && cc<355)
		{
			AQI=Linear(200,151,354,255,cc);
		}
		else if (cc>=355 && cc<425)
		{
			AQI=Linear(300,201,424,355,cc);
		}
		else if (cc>=425 && cc<505)
		{
			AQI=Linear(400,301,504,425,cc);
		}
		else if (cc>=505 && cc<605)
		{
			AQI=Linear(500,401,604,505,cc);
		}
		else
		{
			AQI=OUTRANGE;
		}
		return AQI;
	}
	
	public static int SO2_1hr(float c) { /* unit -> mg/m^3 */
		int AQI;
		c = 22.4f*c/64.06f*1000; /* unit -> ppb */
		if (c>=0 && c<36)
		{
			AQI=Linear(50,0,35,0,c);
		}
		else if (c>=36 && c<76)
		{
			AQI=Linear(100,51,75,36,c);
		}
		else if (c>=76 && c<186)
		{
			AQI=Linear(150,101,185,76,c);
		}
		else if (c>=186 && c<=304)
		{
			AQI=Linear(200,151,304,186,c);
		}
		else if (c>=304 && c<=604)
		{
			AQI=Linear(500,201,604,305,c);
		}
		else
		{
			AQI=OUTRANGE;
		}
		return AQI;
	}
	
	public static int NO2(float c) { /* unit -> mg/m^3 */
		int AQI;
		c = 22.4f*c/46.01f; /* unit -> ppm */
		if (c>=0 && c<.054)
		{
			AQI=Linear(50,0,.053,0,c);
		}
		else if (c>=.054 && c<.101)
		{
			AQI=Linear(100,51,.100,.054,c);
		}
		else if (c>=.101 && c<.361)
		{
			AQI=Linear(150,101,.360,.101,c);
		}
		else if (c>=.361 && c<.645)
		{
			AQI=Linear(200,151,.644,.361,c);
		}
		else if (c>=.645 && c<1.245)
		{
			AQI=Linear(300,201,1.244,.645,c);
		}
		else if (c>=1.245 && c<1.645)
		{
			AQI=Linear(400,301,1.644,1.245,c);
		}
		else if (c>=1.645 && c<=2.044)
		{
			AQI=Linear(500,401,2.044,1.645,c);
		}
		else
		{
			AQI=OUTRANGE;
		}
		return AQI;
	}
	
	public static int SO2_24hr(float c) { /* unit -> mg/m^3 */
		int AQI;
		c = 22.4f*c/64.06f*1000; /* unit -> ppb */
		if (c>=0 && c<=304)
		{
			AQI=Linear(200,0,304,0,c);
		}
		else if (c>=304 && c<605)
		{
			AQI=Linear(300,201,604,305,c);
		}
		else if (c>=605 && c<805)
		{
			AQI=Linear(400,301,804,605,c);
		}
		else if (c>=805 && c<=1004)
		{	
			AQI=Linear(500,401,1004,805,c);
		}
		else
		{
			AQI=OUTRANGE;
		}
		return AQI;
	}
	
	public static int AQICategory(int AQI) {
		int AQICategory = 0;
		if (AQI<=50)
		{
			AQICategory=R.string.aqi_good;
		}
		else if (AQI>50 && AQI<=100)
		{
			AQICategory=R.string.aqi_moderate;
		}
		else if (AQI>100 && AQI<=150)
		{
			AQICategory=R.string.aqi_sensitive;
		}
		else if (AQI>150 && AQI<=200)
		{
			AQICategory=R.string.aqi_unhealthy;
		}
		else if (AQI>200 && AQI<=300)
		{
			AQICategory=R.string.aqi_veryunhealthy;
		}
		else if (AQI>300 && AQI<=400)
		{
			AQICategory=R.string.aqi_hazardous;
		}
		else if (AQI>400 && AQI<=500)
		{
			AQICategory=R.string.aqi_hazardous;
		}
		else
		{
			AQICategory=R.string.aqi_outrange;
		}
		return AQICategory;
	}
	
	public static int AQIAdvisory(int AQI) {
		int advisory = 0;
		if (AQI<=50)
		{
			advisory=R.string.aqi_good_advisory;
		}
		else if (AQI>50 && AQI<=100)
		{
			advisory=R.string.aqi_moderate_advisory;
		}
		else if (AQI>100 && AQI<=150)
		{
			advisory=R.string.aqi_sensitive_advisory;
		}
		else if (AQI>150 && AQI<=200)
		{
			advisory=R.string.aqi_unhealthy_advisory;
		}
		else if (AQI>200 && AQI<=300)
		{
			advisory=R.string.aqi_veryunhealthy_advisory;
		}
		else
		{
			advisory=R.string.aqi_hazardous_advisory;
		}
		
		return advisory;
	}
	
	public static int AQILevel(int AQI) {
		int AQILevel = 0;
		if (AQI<=50)
		{
			AQILevel=1;
		}
		else if (AQI>50 && AQI<=100)
		{
			AQILevel=2;
		}
		else if (AQI>100 && AQI<=150)
		{
			AQILevel=3;
		}
		else if (AQI>150 && AQI<=200)
		{
			AQILevel=4;
		}
		else if (AQI>200 && AQI<=300)
		{
			AQILevel=5;
		}
		else
		{
			AQILevel=6;
		}
		return AQILevel;
	}
	
	public static String AQIColor(int AQI) {
		String color = "#ffffff";
		if (AQI<=50)
		{
			color="#00E400";
		}
		else if (AQI>50 && AQI<=100)
		{
			color="#FFFF00";
		}
		else if (AQI>100 && AQI<=150)
		{
			color="#FF7E00";
		}
		else if (AQI>150 && AQI<=200)
		{
			color="#FF0000";
		}
		else if (AQI>200 && AQI<=300)
		{
			color="#7E0023";
		}
		else
		{
			color="#99004C";
		}
		return color;
	}
	
	public static int getAQI(String name, float value) {
		int aqi = 0;
		if(name.compareTo("pm25")==0) {
			aqi = AQI.PM25(value);
		} else if(name.compareTo("pm10")==0) {
			aqi = AQI.PM10(value);
		} else if(name.compareTo("so2")==0) {
			aqi = AQI.SO2_1hr(value);
		} else if(name.compareTo("no2")==0) {
			aqi = AQI.NO2(value);
		}
		return aqi;
	}
}
